package com.ruoyi.web.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.domain.AppUser;
import com.ruoyi.web.domain.UserAddress;
import com.ruoyi.web.service.IAppUserService;
import com.ruoyi.web.service.IUserAddressService;
import com.ruoyi.web.vo.UserAddressPageVO;
import com.ruoyi.web.vo.UserAddressVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户充值地址Controller
 *
 * @author ruoyi
 * @date 2022-12-30
 */
@RestController
@RequestMapping("/address")
public class UserAddressController extends BaseController {
    @Autowired
    private IUserAddressService userAddressService;
    @Autowired
    private IAppUserService appUserService;

    /**
     * 获取当前用户登录地址
     *
     * @return r
     */
    @GetMapping
    public R<List<UserAddressVO>> getUserAddress() {
        return userAddressService.getUserAddress();
    }

    /**
     * 查询用户充值地址列表
     */
    @PreAuthorize("@ss.hasPermi('web:address:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserAddress userAddress) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser.getUser().getRoles().get(0).getRoleId() == 3) {
            // 获取所有子集
            List<AppUser> userList = new ArrayList<>();
            appUserService.getAllChild(userList, loginUser.getUserId());
            List<Long> userIdList = userList.stream()
                    .map(AppUser::getId)
                    .collect(Collectors.toList());
            if (CollectionUtils.isEmpty(userIdList)) {
                return getDataTable(new ArrayList<>());
            }

            List<UserAddress> list = userAddressService.listByUserIdList(userIdList, userAddress);
            TableDataInfo info = getDataTable(list);

            List<UserAddressPageVO> voList = new ArrayList<>(list.size());
            for (UserAddress i : list) {
                UserAddressPageVO vo = new UserAddressPageVO();
                BeanUtils.copyProperties(i, vo);
                voList.add(vo);
            }

            return getDataTable(info, voList);

        } else {
            startPage();
            List<UserAddress> list = userAddressService.selectUserAddressList(userAddress);
            TableDataInfo info = getDataTable(list);

            List<UserAddressPageVO> voList = new ArrayList<>();
            for (UserAddress user : list) {
                UserAddressPageVO vo = new UserAddressPageVO();
                BeanUtils.copyProperties(user, vo);
                voList.add(vo);
            }
            return getDataTable(info, voList);
        }

    }

    /**
     * 导出用户充值地址列表
     */
    @PreAuthorize("@ss.hasPermi('web:address:export')")
    @Log(title = "用户充值地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserAddress userAddress) {
        List<UserAddress> list = userAddressService.selectUserAddressList(userAddress);
        for (UserAddress address : list) {
            address.setPrivateKey(null);
        }
        ExcelUtil<UserAddress> util = new ExcelUtil<UserAddress>(UserAddress.class);
        util.exportExcel(response, list, "用户充值地址数据");
    }

    /**
     * 获取用户充值地址详细信息
     */
    @PreAuthorize("@ss.hasPermi('web:address:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        UserAddress address = userAddressService.selectUserAddressById(id);
        UserAddressPageVO vo = new UserAddressPageVO();
        BeanUtils.copyProperties(address, vo);
        return success(vo);
    }

    /**
     * 新增用户充值地址
     */
    @PreAuthorize("@ss.hasPermi('web:address:add')")
    @Log(title = "用户充值地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserAddress userAddress) {
        return toAjax(userAddressService.insertUserAddress(userAddress));
    }

    /**
     * 修改用户充值地址
     */
    @PreAuthorize("@ss.hasPermi('web:address:edit')")
    @Log(title = "用户充值地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserAddress userAddress) {
        return toAjax(userAddressService.updateUserAddress(userAddress));
    }

    /**
     * 删除用户充值地址
     */
    @PreAuthorize("@ss.hasPermi('web:address:remove')")
    @Log(title = "用户充值地址", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userAddressService.deleteUserAddressByIds(ids));
    }
}
